package com.changgou.search.controller;


import com.alibaba.fastjson.JSON;
import com.changgou.entity.Result;
import com.changgou.entity.StatusCode;
import com.changgou.order.pojo.Evaluate;
import com.changgou.search.service.ItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/item")
public class ItemController {

    @Autowired
    private ItemService itemService;

    @RequestMapping("/toComment")
    public String toComment(){
        return "item";
    }

    @PostMapping("/{id}")
    @ResponseBody
    public Result findAll(@PathVariable("id")String id){
        List<Evaluate> evaluateList = itemService.findAllById();
        return new Result(true, StatusCode.OK,"查询成功",evaluateList);
    }

    @GetMapping("/count")
    @ResponseBody
    public Result findCount(){
        List<Evaluate> evaluateList = itemService.findAllById();
        Map<String ,Integer> map=new HashMap<>();
        int one=0;
        int three=0;
        int five=0;
        for (Evaluate evaluate : evaluateList) {
            if ("1".equals(evaluate.getEvaluate())){
                one++;
            }
            if ("3".equals(evaluate.getEvaluate())){
                three++;
            }
            if ("5".equals(evaluate.getEvaluate())){
                five++;
            }
        }
        map.put("one",one);
        map.put("three",three);
        map.put("five",five);
        map.put("all",evaluateList.size());
        return new Result(true,StatusCode.OK,"查询成功",map);
    }
}
